<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
    <base href="<%=basePath%>">
    <title>模板查询</title>
    <script src="<%=basePath%>component/layout.js"></script>
    <script src="<%=basePath%>component/toolbar.js"></script>
    <script src="<%=basePath%>component/grid.js"></script>
	<script type="text/javascript" src="<%=basePath%>dwrsystem/engine.js"></script>
	<script type="text/javascript" src="<%=basePath%>dwrsystem/interface/displayService.js"></script>
    
	<script type="text/javascript">
	var mygrid;
	var dhxLayout = createLayoutNormal('1C');
	
	var dhxGrid = {
		initDataTable : function () {
		    mygrid.setHeader("序号,模板描述,模板地址,默认");
		    mygrid.setInitWidths("50,150,*,50");
		    mygrid.setColAlign("right,left,left,center");
		    mygrid.setColSorting("int,str,str,str");
		    mygrid.setColTypes(power.getPowerUpdate()?"ro,ed,ed,ro":"ro,ro,ro,ro");
		    mygrid.attachEvent("onEditCell", this.onEditCell);
		    mygrid.init();
		    setSkin(mygrid);
		    mygrid.loadXML('<%=basePath%>system-display/DisplayAction!displayGrid.action');
		},
		onEditCell : function (stage,rId,cInd,nValue,oValue){
			if(stage == 2 && nValue != oValue){
				if(cInd == '1'){
					layoutProgressOn(dhxLayout);
					displayService.updateTDisplayByPK({frameModelId:rId,frameModelDesc:nValue},function(data){
						parent.notifyCreater('模版描述修改成功');
						layoutProgressOff(dhxLayout);			
					});
				}
				if(cInd == '2'){
					layoutProgressOn(dhxLayout);
					displayService.updateTDisplayByPK({frameModelId:rId,frameModelWay:nValue},function(data){
						parent.notifyCreater('模版地址修改成功');
						layoutProgressOff(dhxLayout);
					});
				}
		   	}
		    return true;
		},
		setDefault : function (){
			if(mygrid.getSelectedId()){
				layoutProgressOn(dhxLayout);
				displayService.updateTDisplayDefaultModelAndAnotherNormal({frameModelId:mygrid.getSelectedId(),frameDefault:'1'},function(data){
					for(var i=0 ;i<(mygrid.getRowsNum());i++){
						var v = mygrid.cells(mygrid.getRowId(''+i),3).getValue();
						if(v == 1 && mygrid.getRowId(''+i) != mygrid.getSelectedId()){
							 mygrid.cells(mygrid.getRowId(''+i),3).setValue('0');
						}
						if(mygrid.getRowId(''+i) == mygrid.getSelectedId()){
							 mygrid.cells(mygrid.getRowId(''+i),3).setValue('1');
						}
					}
					parent.notifyCreater('设置成功');
					layoutProgressOff(dhxLayout);
				});
			} else parent.notifyWarn('请选择设置项');
		},
		createInfo : function (){
			layoutProgressOn(dhxLayout);
			displayService.insertByTDisplay({frameModelId:''},function(data){
				var sortnumber = mygrid.getRowsNum()+1;
				mygrid.addRow(data, ''+(mygrid.getRowsNum()+1), ''+mygrid.getRowsNum());
				mygrid.selectRowById(data);
				parent.notifyCreater('添加成功');
				layoutProgressOff(dhxLayout);
			});
		},
		deleteInfo : function (){
			if(mygrid.getSelectedId()){
				var validateUpdate = false;
				for(var i=0 ;i<(mygrid.getRowsNum());i++){
					var v = mygrid.cells(mygrid.getRowId(''+i),3).getValue();
					if(v == 1 && mygrid.getRowId(''+i) != mygrid.getSelectedId()){
						validateUpdate = true;
						break;
					}
				}
				if(!validateUpdate){
					parent.notifyWarn("无默认值,不能删除");
					return false;
				}
				confirmInfo('确认删除？',
					function(choose){
						if('ok' == choose){
							layoutProgressOn(dhxLayout);
							displayService.deleteTDisplayByPK({frameModelId:mygrid.getSelectedId()},function(data){
								var lastrowIndex = mygrid.getRowIndex(mygrid.getSelectedId())-1;
								mygrid.deleteSelectedRows();
								mygrid.selectRow(lastrowIndex);
								
								//修改前面的序号
								for(var i=(lastrowIndex+1) ;i<(mygrid.getRowsNum());i++){
									mygrid.cells(mygrid.getRowId(''+i),0).setValue(i+1);
								}
								layoutProgressOff(dhxLayout);parent.notifyCreater('删除成功');
							});
						}
					}
				);
			} else parent.notifyWarn('请选择删除项');
		}
	};
	
	function configLayout(){
		var a = dhxLayout.cells('a');
		a.hideHeader();
		a.setText('主页面');
		a.fixSize(true, true);
		mygrid = createGridFromLayoutCell(a);
		if(power.getPower() != '' && (power.getPowerInsert() || power.getPowerDelete()))
			createToolbarFromDHXLayoutCell(a,function(toolbar){
			    toolbar.addText("info", 1, "操作:");
			    toolbar.addSeparator("sep1", 2);
			    if(power.getPowerInsert())toolbar.addButton("add", 3, "新增", "new.gif", "new_dis.gif");
			    if(power.getPowerDelete())toolbar.addButton("delete", 4, "删除", "cut.gif", "cut_dis.gif");
			    if(power.getPowerUpdate())toolbar.addButton("setDefault", 5, "设为默认", "settings.gif", "settings.gif");
			    toolbar.attachEvent("onClick", function(id) {
			    	if(id == 'add'){
			    		dhxGrid.createInfo();
			    	}
			    	if(id == 'delete'){
			    		dhxGrid.deleteInfo();
			    	}
			    	if(id == 'setDefault'){
			    		dhxGrid.setDefault();
			    	}
			    });
			});
	}
	
	$(function (){
		configLayout();
	 	dhxGrid.initDataTable();
	});
	</script>
</head>
  
<body>
</body>

</html>
